package com.zsd.android.opengldemo.view.activity;

import android.Manifest;
import android.app.PendingIntent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

import com.zsd.android.opengldemo.R;
import com.zsd.android.opengldemo.handler.DBHandler;
import com.zsd.android.opengldemo.interfaces.DBHelper;
import com.zsd.android.opengldemo.utils.Contacts;
import com.zsd.android.opengldemo.utils.MLog;
import com.zsd.android.opengldemo.utils.PermissionHelper;
import com.zsd.android.opengldemo.view.widget.FGLView;

import java.lang.ref.WeakReference;
import java.lang.reflect.Proxy;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements DBHelper, View.OnClickListener {

    private FGLView mGlView;
    private ImageView bellIv;

    private DBHelper dbHelper;
    private Animation animation;
    private BellAnimHandler animHandler;
    private PendingIntent sentIntent, deliveryIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGlView = findViewById(R.id.main_glView);
        bellIv = findViewById(R.id.bell_anim_iv);
        bellIv.setOnClickListener(this);

        dbHelper = (DBHelper) Proxy.newProxyInstance(DBHelper.class.getClassLoader(), new Class[]{DBHelper.class}, new DBHandler(this));
        dbHelper.delete();

        animation = AnimationUtils.loadAnimation(this, R.anim.bell_shake_anim);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animHandler.sendEmptyMessageDelayed(0, 1000);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animHandler = new BellAnimHandler(this);
        animHandler.sendEmptyMessage(0);

        PermissionHelper.getInstance(this).isPermission(this, new String[]{Manifest.permission.SEND_SMS}, 200);
    }

    @Override
    public void save() {
        MLog.i("save()");
    }

    @Override
    public void delete() {
        MLog.i("delete()");
    }

    @Override
    public void update() {
        MLog.i("update()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGlView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGlView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        animHandler.detach();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bell_anim_iv:
                sendSMS(Contacts.PHONE_NUMBER, Contacts.SMS_MESSAGE);
                break;
        }
    }

    private void sendSMS(String phoneNumber, String message) {
        if (TextUtils.isEmpty(message)) {
            return;
        }
        SmsManager smsManager = SmsManager.getDefault();
        ArrayList<String> messages = smsManager.divideMessage(message);
        for (String txt : messages) {
            Toast.makeText(this, txt, Toast.LENGTH_SHORT).show();
            smsManager.sendTextMessage(phoneNumber, null, txt, null, null);
        }
    }

    public static class BellAnimHandler extends Handler {

        private WeakReference<MainActivity> mReference;

        public BellAnimHandler(MainActivity activity) {
            mReference = new WeakReference<>(activity);
        }

        public void detach() {
            if (mReference != null) {
                mReference.clear();
                mReference = null;
                System.gc();
            }
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            MainActivity activity = mReference.get();
            switch (msg.what) {
                case 0:
                    activity.bellIv.clearAnimation();
                    activity.bellIv.startAnimation(activity.animation);
                    break;
                case 1:
                    MLog.i("animation cancel");
//                    activity.animation.cancel();
//                    activity.animation.setRepeatCount(1);
                    activity.bellIv.clearAnimation();
                    break;
            }
        }
    }
}
